package com.boot.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;


public class IO_read_and_write {
/*	stringBuilder
 * 	开始执行读取excel文件方法
	读取的Excel文件地址为: d://111/ok.xls
	读取excel文件总计用时: 351 毫秒

	追加内容到指定txt
	开始向d:\testSpeed.txt中写入数据
	写入txt文件总计用时21毫秒

	总计用时 373 毫秒
	finish*/
/*
 * 	string
	开始执行读取excel文件方法
	读取的Excel文件地址为: d://111/ok.xls
	读取excel文件总计用时: 344 毫秒

	追加内容到指定txt
	开始向d:\testSpeed.txt中写入数据
	写入txt文件总计用时1152毫秒

	总计用时 1497 毫秒
	finish*/

	
	/** 设置读取文件路径 */
	private static final String PATHNAME = "d:\\drugcode.txt";
	/** 设置读取文件字符编码 */
	//GBk   UTF-8
	private static final String ENCODED = "UTF-8";
	/** 读取文件行数 */
	private static int TXT_LINE = 0;//要读取多少行,若果读取全文,则为0
	/** 设置写入文件路径 */
	private static final String WRITE_PATHNAME = "d:\\afterFormat.txt";

	/**
	 * <p>
	 * 读取本地的txt文件
	 * </p>
	 * 
	 * @author： wangqiang
	 * @日期：2016-6-3 上午8:57:40
	 * @param readPath 读取文件路径,传""使用默认路径
	 * @return
	 */
	public static List<String> readTxtFile(String readPath) {
		long startTime = System.currentTimeMillis();
		//如果传入路径不为空,则使用传入路径,否则使用默认路径
		String txtPath = PATHNAME;
		if (!StringUtils.isEmpty(readPath)) {
			txtPath = readPath;
		}
		System.out.println("开始读取文件" + txtPath);
		File file = new File(txtPath);
		if (file.isFile() && file.exists()) {
			try {
				InputStreamReader isReader = new InputStreamReader(new FileInputStream(file), ENCODED);
				BufferedReader buf = new BufferedReader(isReader);
				List<String> list = new ArrayList<>();
				String lineTxt = "";
				if (TXT_LINE != 0) {
					for (int i = 0; i < TXT_LINE; i++) {
						lineTxt = buf.readLine();
						list.add(lineTxt);
					}
				} else {
					//读取文件到最后一行,并将行数累计进TXT_LINE
					while ((lineTxt = buf.readLine()) != null) {
						list.add(lineTxt);
						TXT_LINE++;
					}
				}
				System.out.println("读取一共  " + TXT_LINE + " 行数据");
				buf.close();
				long endTime = System.currentTimeMillis();
				System.out.println("读取txt文件总计用时:" + (endTime - startTime) + " 毫秒\n");
				return list;

			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				System.out.println("不支持编码");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				System.out.println("文件不存在或者有错!");
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("io出错");
			}
		}
		return new ArrayList<>();
	}

	/**
	 * <p>
	 * B方法追加文件：在指定文件的内容后面追加内容,使用FileWriter
	 * </p>
	 * 
	 * @author： wangqiang
	 * @日期：2016-6-7 上午9:25:59
	 * @param contentList  传入写入队列,如不传则传null
	 * @param content	传入写入字符串,不穿则为""
	 * @param write_pathName 输出路径,可传""
	 */
	public static void writeTxtFileAppen(List<String> contentList, String content, String write_pathName) {
		try {
			long startTime = System.currentTimeMillis();//开始计时
			StringBuilder sb = new StringBuilder();
			//传入的content不为空
			if (!StringUtils.isEmpty(content)) {
				//换行
				sb.append(content + "\r\n");
			}
			//传入的contentList不为空
			if (contentList != null && contentList.size() > 0) {
				Iterator<String> it = contentList.iterator();
				while (it.hasNext()) {
					sb.append(it.next() + "\r\n");
				}
			}
			System.out.println("追加内容到指定txt");
			String writePath = WRITE_PATHNAME;
			//判断传入的输出路径是否为空,为空则使用默认路径
			if (!StringUtils.isEmpty(write_pathName)) {
				writePath = write_pathName;
			}
			System.out.println("开始向" + writePath + "中写入数据");
			//打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
			FileWriter writer = new FileWriter(writePath, true);
			
			writer.write(sb.toString());
			writer.close();
			long endTime = System.currentTimeMillis();
			System.out.println("写入txt文件总计用时" + (endTime - startTime) + "毫秒\n");
		} catch (IOException e) {
			System.out.println("IO_read_and_write--writeTxtFileAppen追加文本到txt出错");
			e.printStackTrace();
		}
	}

	/**
	 * <p>
	 * A方法创建或覆盖文件:传入要输出的内容,和输出路径,如果路径为空则用默认路径
	 * </p>
	 * 
	 * @author： wangqiang
	 * @日期：2016-6-7 上午9:22:55
	 * @param content
	 * @param write_pathName 输出路径,可传""
	 */
	public static void writeTxtFile(List<String> contentList, String content, String write_pathName) {
		long startTime = System.currentTimeMillis();
		StringBuilder sb = new StringBuilder();
		//传入的contentList不为空
		if (contentList != null && contentList.size() > 0) {
			Iterator<String> it = contentList.iterator();
			while (it.hasNext()) {
				sb.append(it.next() + "\r\n");
			}
		}
		System.out.println("写入txt, 创建或覆盖文件");
		String writePath = WRITE_PATHNAME;
		//判断传入的输出路径是否为空,为空则使用默认路径
		if (!StringUtils.isEmpty(write_pathName)) {
			writePath = write_pathName;
		}
		System.out.println("开始向" + writePath + "中写入数据");
		File file = new File(writePath);

		OutputStream write_out = null;
		try {
			write_out = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			System.out.println("IO_read_and_write--writeTxtFile  创建OutputStream出错");
			e.printStackTrace();
		}

		//传入的content不为空
		if (!StringUtils.isEmpty(content)) {
			//换行
			sb.append(content + "\r\n");
		}
		
		// 将字符串转成字节数组
		byte b[] = sb.toString().getBytes();

		try {
			// 将byte数组写入到文件之中
			write_out.write(b);
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			try {
				//关闭输出流
				write_out.close();
				long endTime = System.currentTimeMillis();
				System.out.println("写入txt文件总计用时" + (endTime - startTime) + "毫秒\n");
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		}

	}
}
